<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="chart" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树状图 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <template #default="{ data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px ;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="chart" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="dodele(data.id)">删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 弹层 -->
      <el-dialog :title="isEdit?'编辑':'新增'" :visible.sync="dialogFormVisible">
        <dldialog
          v-if="dialogFormVisible"
          :cur-id="curId"
          :is-edit="isEdit"
          :originlist="originlist"
          @success="hsuccos"
          @close="close"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { tranListToTreeData } from '@/utils/index'
import { getDepartments, deletpartments } from '@/api/departments'
import dldialog from './dialong.vue'
export default {
  components: {
    dldialog
  },
  data() {
    return {
      curId: '',
      isEdit: false,
      list: [],
      // 弹层
      dialogFormVisible: false,
      originlist: []
    }
  },
  created() {
    // 树状图函数
    this.loadDepartments()
  },
  methods: {
    // 树状图函数
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res, '部门树状')
        // 去掉第一个重复数据
        res.data.depts.shift()
        this.list = tranListToTreeData(res.data.depts)
        this.originlist = res.data.depts.map(({ code, id, pid, name }) => {
          return { code, id, pid, name }
        })
        console.log(res, '部门信息')
      } catch (error) {
        console.log(error)
      }
    },
    // 确认删除
    dodele(id) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deletpartments(id)
        this.$message.success('删除成功')
        this.loadDepartments()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 子传父函数
    hsuccos() {
      this.loadDepartments()
      this.dialogFormVisible = false
    },
    // 添加
    hAdd(id) {
      this.isEdit = false
      this.dialogFormVisible = true
      this.curId = id
    },
    // 编辑
    hEdit(id) {
      this.isEdit = true
      this.dialogFormVisible = true
      this.curId = id
    },
    close() {
      this.dialogFormVisible = false
    }
  }
}
</script>
